<?php

namespace App\Http\Controllers\Admin;

use App\Model\Orders;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndexController extends Controller
{
    /**
     * @return mixed
     * 后台首页
     */
    public function index(){
        $orders = new Orders();
        $saleInfo = array();
        $saleTime = array();
        for($i=0;$i<7;$i++){
            $date = date("Y-m-d",time()-24*60*60*$i);
            $saleInfo[$i] = $this->saleData($date);
            $saleTime[$i] = $date;
        }
        $allSales = $orders->select(DB::raw('sum(real_price) as allSales'))->where('status','>',0)->where('created_at','like','%'.date("Y-m").'%')->first()->allSales;
        $ordersCount = $orders->where('status','>',0)->where('created_at','like','%'.date("Y-m").'%')->count();
        return view('admin/index/index',[
            'saleInfo' => $saleInfo,
            'saleTime' => $saleTime,
            'allSales' => $allSales,
            'ordersCount' => $ordersCount
        ]);
    }

    /**
     * @return mixed
     * 最近七天的数据
     */
    public function saleData($date){
        $orders = new Orders();
        $data = $orders->select(DB::raw('sum(real_price) as sales'))->where('status','>','0')->where('created_at','like','%'.$date.'%')->first()->sales;
        return empty($data)? 0:$data;
    }
    public function skinConfig(){
        return view("admin.index.skin-config");
    }
}
